//
//  ZFBHomeTopView.m
//  支付宝
//
//  Created by HaoYoson on 16/6/27.
//  Copyright © 2016年 HaoYoson. All rights reserved.
//

#import "Masonry.h"
#import "NSAttributedString+Additon.h"
#import "UIColor+Addition.h"
#import "ZFBHomeTopView.h"

@implementation ZFBHomeTopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)awakeFromNib {
    [self setupUI];
}

- (void)setupUI {
    self.backgroundColor = [UIColor colorWithHex:0x3a3a3a];

    UIButton *btn1 = [self loadTopButtonWithTitle:@"扫一扫" andImageName:@"home_scan" andButtonType:HomeTopButtonTypeScan];
    UIButton *btn2 = [self loadTopButtonWithTitle:@"付款" andImageName:@"home_pay" andButtonType:HomeTopButtonTypePay];
    UIButton *btn3 = [self loadTopButtonWithTitle:@"卡卷" andImageName:@"home_card" andButtonType:HomeTopButtonTypeCard];
    UIButton *btn4 = [self loadTopButtonWithTitle:@"咻一咻" andImageName:@"home_xiu" andButtonType:HomeTopButtonTypeXiu];

    [self addSubview:btn1];
    [self addSubview:btn2];
    [self addSubview:btn3];
    [self addSubview:btn4];

    [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
    }];

    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
    }];

    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn1.mas_right);
        make.width.equalTo(btn1);
    }];

    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn2.mas_right);
        make.width.equalTo(btn2);
    }];

    [btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn3.mas_right);
        make.width.equalTo(btn3);
        make.right.offset(0);
    }];
}

- (UIButton *)loadTopButtonWithTitle:(NSString *)title andImageName:(NSString *)imageName andButtonType:(HomeTopButtonType)type {
    UIButton *button = [[UIButton alloc] init];

    NSAttributedString *str = [NSAttributedString imageTextWithImage:[UIImage imageNamed:imageName] imageWH:50 title:title fontSize:14 titleColor:[UIColor whiteColor] spacing:8];

    [button setAttributedTitle:str forState:UIControlStateNormal];

    // 设置多行文本
    button.titleLabel.numberOfLines = 0;
    // 设置文本居中
    button.titleLabel.textAlignment = NSTextAlignmentCenter;

    [button sizeToFit];

    button.tag = type;

    [button addTarget:self action:@selector(topViewClick:) forControlEvents:UIControlEventTouchUpInside];

    return button;
}

- (void)topViewClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(homeTopView:didClickWithButtonType:)]) {
        [self.delegate homeTopView:self didClickWithButtonType:sender.tag];
    }
}

@end
